import constants from "./constants";
import global from "./global";
import anim_util from "./anim_util";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

declare const wx;

@ccclass
export default class NewClass extends cc.Component {

    userInfoBtn:any;

    // LIFE-CYCLE CALLBACKS:

     onLoad () {
        anim_util.runAnimSmallToBigForever(this.node);
     }

    start () {

    }

    onEnable(){
        if (CC_WECHATGAME) {
            let wxInfo= wx.getSystemInfoSync();
            let scaleX=wxInfo.screenWidth/this.node.getParent().width;
            let scaleY=wxInfo.screenHeight/this.node.getParent().height;
            let pos=this.node.getParent().convertToWorldSpaceAR(this.node.getPosition());
            if(!this.userInfoBtn){
                this.userInfoBtn =  wx.createUserInfoButton({
                    type: 'text',
                    text: '',
                    style: {
                      left: pos.x*scaleX-this.node.width*scaleX/2,
                      top: pos.y*scaleY-this.node.height*scaleY/2,
                      width: this.node.width*scaleX,
                      height: this.node.height*scaleY,
                      backgroundColor: '#00000001',
                      color: '#00000001',
                      textAlign: 'center'
                    }
                  });
                  this.userInfoBtn.onTap((res) => {
                    if (res.userInfo) {
                      this.userInfoBtn.destroy();
                      cc.log('授权成功！')
                      global.userInfo=res.userInfo;
                      cc.director.loadScene("main");
                    }
                  });
            }
               this.userInfoBtn.show();
            }
    }
    onDisable(){
        this.userInfoBtn.destroy();
    }

    onStartGameClick(){
        cc.log(this.node.getPosition(),this.node.getParent().convertToWorldSpaceAR(this.node.getPosition()),this.node.width,this.node.height);


        cc.director.emit(constants.GAME_EVENT.BtnClick);
        if (!CC_WECHATGAME) {
            cc.director.loadScene("main");
        }
       
    }
}
